class Solution(object):
    def hasAlternatingBits(self, n):
        """
        :type n: int
        :rtype: bool
        """
        l = n & -n
        if l > 2:
            return False
        n = n & (n - 1)
        while n > 0:
            r = n & -n
            if r >> 2 != l:
                return False
            n = n & (n - 1)
            l = r
        return True
